package com.heiyu.controller;

import com.google.common.collect.Maps;
import com.heiyu.controller.request.UserDelRequest;
import com.heiyu.controller.request.UserGetRequest;
import com.heiyu.controller.request.UserListRequest;
import com.heiyu.controller.request.UserUpdateRequest;
import com.heiyu.controller.response.*;
import com.heiyu.dao.UserInfoMapper;
import com.heiyu.entity.UserInfo;
import com.heiyu.utils.PageHelper;
import com.heiyu.utils.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

@Api
@RestController
@RequestMapping("user")
public class UserController extends AdminBaseController {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @ApiOperation(value = "list",notes = "用户列表")
    @RequestMapping(value = "list",method = RequestMethod.GET)
    public UserListResponse list(@ModelAttribute UserListRequest request){
        UserListResponse response = new UserListResponse();
        response.success();
        Map<String,Object> param = Maps.newHashMap();
        if(StringUtils.isNoneBlank(request.getNickName())||StringUtils.isNoneBlank(request.getRealName())||StringUtils.isNoneBlank(request.getTelphone())){
            param.put("nickName",request.getNickName());
            param.put("realName",request.getRealName());
            param.put("telphone",request.getTelphone());
        }
        param = PageHelper.page(userInfoMapper,param,request.getPageIndex(),request.getPageSize());
        response.setData(param);
        return response;
    }

    @ApiOperation(value = "update",notes = "更新用户信息")
    @RequestMapping(value = "update",method = RequestMethod.POST)
    public UserUpdateResponse update(@ModelAttribute UserUpdateRequest request){
        UserUpdateResponse response= new UserUpdateResponse();
        UserInfo userInfo = this.userInfoMapper.selectPrimary(request.getUserId());
        if(userInfo == null){
            response.error("数据不存在");
        }
        userInfo.setNickName(request.getNickName());
        userInfo.setStatus(request.getStatus());
        userInfo.setAvator(request.getAvator());
        if(this.userInfoMapper.updateById(userInfo)>0){
            response.success();
        }else{
            response.error("更新数据库失败");
        }
        return response;
    }

    public UserDelResponse del(@RequestBody UserDelRequest request){
        return null;
    }

    @ApiOperation("新增用户")
    @PostMapping("put")
    public BaseResponse put(@ModelAttribute UserInfo userInfo){
        BaseResponse response  = new BaseResponse();
        if(StringUtils.isBlank(userInfo.getNickName())||StringUtils.isBlank(userInfo.getRealName())||StringUtils.isBlank(userInfo.getAvator())){
            response.error("参数不正确");
        }else{
            if(StringUtils.isBlank(userInfo.getTelphone())){
                userInfo.setTelphone(userInfo.getNickName());
            }
            userInfo.setCreatedTime(new Date());
            userInfo.setModifyTime(new Date());
            userInfo.setIsDeleted("0");
            this.userInfoMapper.insert(userInfo);
            response.success();
        }
        return response;
    }

    @ApiOperation(value = "get",notes = "获取用户详情")
    @RequestMapping(value = "get",method = RequestMethod.GET)
    public UserGetResponse get(@ModelAttribute UserGetRequest request){
        UserGetResponse response = new UserGetResponse();
        UserInfo userInfo = this.userInfoMapper.selectPrimary(Long.valueOf(request.getTargetId()));
        if(userInfo == null){
            response.error("数据不存在");
        }else{
            response.success();
            response.setData(userInfo);
        }
        return response;
    }
}
